package concurrent.callable;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

public class CallableTest {

	public static void main(String[] args) throws ExecutionException, InterruptedException {
		FutureTask<Integer> futureTask = new FutureTask<>(new CallableThread());

		//多个线程执行 一个FutureTask的时候，只会计算一次
		new Thread(futureTask, "线程2").start();
		new Thread(futureTask, "线程1").start();

		System.out.println("结果为" + futureTask.get());
	}

}

class CallableThread implements Callable<Integer> {

	@Override
	public Integer call() throws Exception {

		Random random = new Random();
		int i = random.nextInt(10);
		System.out.println(Thread.currentThread().getName() + "call in " + i);
		return i;
	}
}
